package com.sky.happygos.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.TResult;
import com.sky.happygos.R;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.ShopE;
import com.sky.happygos.event.UpdateShopE;
import com.sky.happygos.event.UpdateUserE;
import com.sky.happygos.event.UserMeE;
import com.sky.happygos.model.Shop;
import com.sky.happygos.model.User;
import com.sky.happygos.present.ShopP;
import com.sky.happygos.present.UserP;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/22.
 */
@EActivity(R.layout.activity_user_info)
public class UserInfoA extends TakePhotoActivity {

    @ViewById(R.id.loadView)
    LoadView loadView;
    @ViewById(R.id.sdv_user_info_icon)
    SimpleDraweeView icon;
    @ViewById(R.id.et_user_info_phone)
    EditText phone;
    @ViewById(R.id.et_user_info_name)
    EditText name;
    @ViewById(R.id.et_user_info_desc)
    EditText desc;

    @ViewById(R.id.header_right)
    ImageView commit;


    @Bean
    UserP userP;

    User user;

    String path = null;
    boolean isChange = false;

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            isChange = true;
            commit.setVisibility(View.VISIBLE);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };


    @AfterViews
    public void init(){

        loadView.start();
        userP.getUser();
    }

    @Click(R.id.header_right)
    public void commit(){
        User u = new User();
        u.id = user.id;
        u.name = name.getText().toString();
        u.info = desc.getText().toString();

        loadView.start();
        userP.updateUser(path,u);
//        shopP.updateShop(path,s);
    }

    @Click(R.id.sdv_user_info_icon)
    public void clickPhoto(){
        TakePhoto takePhoto = getTakePhoto();
        CompressConfig compressConfig=new CompressConfig.Builder().setMaxSize(600*800).setMaxPixel(800).create();
        takePhoto.onEnableCompress(compressConfig,true);
        takePhoto.onPickFromGallery();
    }

    public void loadData(){
        icon.setImageURI(ImagePath.to(user.icon));

        name.setText(user.name);
        desc.setText(user.info);
        phone.setText(user.phone);

        name.addTextChangedListener(watcher);
        desc.addTextChangedListener(watcher);

    }

    @Subscribe
    public void getUserResult(UserMeE e){
        loadView.dismiss();
        if(e.isSuccess){
                user = e.data;
                loadData();
        }else{

            Toast.makeText(this,"获取用户信息失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe
    public void updateUserResult(UpdateUserE e){
        loadView.dismiss();
        if(e.isSuccess){
            Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
            finish();
        }else{
            Toast.makeText(this,"修改失败",Toast.LENGTH_SHORT).show();
        }
    }


    @Click(R.id.header_left)
    public void back(){
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        path = result.getImage().getCompressPath();
        icon.setImageURI(String.format("file://%s",result.getImage().getCompressPath()));
        isChange = true;
        commit.setVisibility(View.VISIBLE);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        Toast.makeText(this,"获取失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
        Toast.makeText(this,"获取取消",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
